local skill = fk.CreateSkill {
  name = "lvshi__await_exhausted_skill",
}

local ls = require "packages/lvshi/utility"

skill:addEffect("cardskill", {
  prompt = "#lvshi__await_exhausted",
  mod_target_filter = function(self, player, to_select, selected, card, distance_limited)
    return ls.isFriend(player, to_select)
  end,
  can_use = function(self, player, card)
    if player:prohibitUse(card) then return end
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if not player:isProhibited(p, card) and self:modTargetFilter(player, p, {}, card, true) then
        return true
      end
    end
  end,
  on_use = function(self, room, use)
    if not use.tos or #use.tos == 0 then
      local player = use.from
      if not player.role_shown then
        use.tos = { use.from }
      else
        use.tos = {}
        for _, p in ipairs(room:getAlivePlayers()) do
          if not player:isProhibited(p, use.card) and isFriend(p, player) then
            use:addTarget(p)
          end
        end
      end
    end
  end,
  on_effect = function(self, room, effect)
    local target = effect.to
    if target.dead then return end
    target:drawCards(2, skill.name)
    if target.dead then return end
    room:askToDiscard(target, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = skill.name,
      cancelable = false,
    })
  end,
})

return skill
